package client;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.browser.BrowserFunction;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.List;
import org.eclipse.swt.widgets.Shell;


/**
 * Used to create a window which has the local browser embedded.
 */
public class BrowserClient {

	private static final Logger logger = Logger.getLogger(BrowserClient.class.getName());
	private static final String serverURL = StaticDefinitions.serverURL;
	private static final String testURL = "www.reddit.com";

	private List list;
	private Browser browser;
	
	protected BrowserClient() {
		initialize(serverURL);
	}
	
	private BrowserClient(String url) {
		initialize(url);
	}
	
	private void initialize(String url) {
		Display display = new Display();
		final Shell shell = new Shell(display);
		shell.setLayout(new FillLayout());
		SashForm sash = new SashForm(shell, SWT.VERTICAL);

		try {
			// Change to MOZILLA if you can ever figure out the "handle" issue
			browser = new Browser(sash, SWT.NONE);
		} catch (SWTError e) {
			logger.log(Level.SEVERE, "Could not instantiate Browser:", e);
			display.dispose();
			return;
		}
		
		/*browser.addControlListener(new ControlListener() {
			public void controlResized(ControlEvent e) {
				browser.execute("document.getElementById('map_canvas').style.width= "
						+ (browser.getSize().x - 20) + ";");
				browser.execute("document.getElementById('map_canvas').style.height= "
						+ (browser.getSize().y - 20) + ";");
			}
			public void controlMoved(ControlEvent e) {
			}
		});*/

		new CustomFunction(browser, "theJavaFunction");

		Composite c = new Composite(sash, SWT.BORDER);
		c.setLayout(new GridLayout(1, true));
		
		// Keeping these for the moment as examples of what can be done
		/*Button b = new Button(c, SWT.PUSH);
		list = new List(c, SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		list.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		b.setText("Where Am I ?");
		b.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				double lat = ((Double) browser
						.evaluate("return map.getCenter().lat();"))
						.doubleValue();
				double lng = ((Double) browser
						.evaluate("return map.getCenter().lng();"))
						.doubleValue();
				list.add(lat + " : " + lng);
			}
		});*/

		browser.setUrl(url);
		sash.setWeights(new int[] { 1, 0 });
		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
	
	/**
	 * Used for testing exclusively.
	 * @param args not used
	 */
	public static void main(String args[]) {
		@SuppressWarnings("unused")
		BrowserClient bc = new BrowserClient(testURL);
	}

	class CustomFunction extends BrowserFunction {
		Browser browser = null;

		CustomFunction(Browser browser, String name) {
			super(browser, name);
			this.browser = browser;
		}

		public Object function(Object[] arguments) {
			double lat = ((Double) arguments[0]).doubleValue();
			double lng = ((Double) arguments[1]).doubleValue();
			list.add(lat + " : " + lng);
			browser.execute("document.getElementById('map_canvas').style.width= "
					+ (browser.getSize().x - 20) + ";");
			browser.execute("document.getElementById('map_canvas').style.height= "
					+ (browser.getSize().y - 20) + ";");
			return null;
		}
	}

}
